package com.example.bottomnavigationkotlin.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bottomnavigationkotlin.R
import com.example.bottomnavigationkotlin.model.HomeItem
import com.example.bottomnavigationkotlin.model.FuncItem
import com.google.android.material.tabs.TabLayoutMediator

class HomeAdapter(
    private val items: List<HomeItem>,
    private val onItemClick: (HomeItem) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_BANNER = 0
        private const val TYPE_FUNC_GRID = 1
        private const val TYPE_DECISION = 2
        private const val TYPE_PRODUCT = 3
    }

    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is HomeItem.Banner -> TYPE_BANNER
            is HomeItem.FuncGrid -> TYPE_FUNC_GRID
            is HomeItem.Decision -> TYPE_DECISION
            is HomeItem.Product -> TYPE_PRODUCT
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_BANNER -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_home_banner, parent, false)
                BannerViewHolder(view)
            }
            TYPE_FUNC_GRID -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_home_func_grid, parent, false)
                FuncGridViewHolder(view)
            }
            TYPE_DECISION -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_home_decision, parent, false)
                DecisionViewHolder(view)
            }
            TYPE_PRODUCT -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_home_product, parent, false)
                ProductViewHolder(view)
            }
            else -> throw IllegalArgumentException("Unknown view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = items[position]) {
            is HomeItem.Banner -> (holder as BannerViewHolder).bind(item)
            is HomeItem.FuncGrid -> (holder as FuncGridViewHolder).bind(item)
            is HomeItem.Decision -> (holder as DecisionViewHolder).bind(item)
            is HomeItem.Product -> (holder as ProductViewHolder).bind(item)
        }
    }

    override fun getItemCount() = items.size

    inner class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val viewPager = itemView.findViewById<androidx.viewpager2.widget.ViewPager2>(R.id.viewPager)
        private val tabLayout = itemView.findViewById<com.google.android.material.tabs.TabLayout>(R.id.tabLayout)
        private val bannerAdapter = BannerAdapter()

        init {
            viewPager.adapter = bannerAdapter
            TabLayoutMediator(tabLayout, viewPager) { _, _ -> }.attach()
        }

        fun bind(item: HomeItem.Banner) {
            bannerAdapter.submitList(item.images)
        }
    }

    inner class FuncGridViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val recyclerFuncGrid = itemView.findViewById<RecyclerView>(R.id.recyclerFuncGrid)
        fun bind(item: HomeItem.FuncGrid) {
            recyclerFuncGrid.layoutManager = GridLayoutManager(itemView.context, 5)
            recyclerFuncGrid.adapter = FuncAdapter(item.funcs) { funcItem ->
                // 这里可以加单独的点击回调，也可以用onItemClick
            }
        }
    }

    inner class DecisionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val titleTextView = itemView.findViewById<TextView>(R.id.tv_decision_title)
        private val subtitleTextView = itemView.findViewById<TextView>(R.id.tv_decision_subtitle)
        private val decisionButton = itemView.findViewById<View>(R.id.ll_decision_btn)

        fun bind(item: HomeItem.Decision) {
            titleTextView.text = item.title
            subtitleTextView.text = item.subtitle
            decisionButton.setOnClickListener { onItemClick(item) }
        }
    }

    inner class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView = itemView.findViewById<ImageView>(R.id.iv_product)
        private val titleTextView = itemView.findViewById<TextView>(R.id.tv_product_title)
        private val priceTextView = itemView.findViewById<TextView>(R.id.tv_product_price)

        fun bind(item: HomeItem.Product) {
            imageView.setImageResource(item.imageResId)
            titleTextView.text = item.title
            priceTextView.text = item.price
            itemView.setOnClickListener { onItemClick(item) }
        }
    }
}

class BannerAdapter : RecyclerView.Adapter<BannerAdapter.BannerViewHolder>() {
    private var images: List<Int> = emptyList()

    fun submitList(newImages: List<Int>) {
        images = newImages
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_banner_image, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
        holder.bind(images[position])
    }

    override fun getItemCount() = images.size

    class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView = itemView.findViewById<ImageView>(R.id.iv_banner)

        fun bind(imageResId: Int) {
            imageView.setImageResource(imageResId)
        }
    }
} 